#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>

#define ERR_EXIT(m) \
    do { \
        perror(m); \
        exit(EXIT_FAILURE); \
    } while (0)

int main () {
    int sockfds[2];

    if (socketpair(PF_UNIX, SOCK_STREAM, 0, sockfds) < 0) {
        ERR_EXIT("socketpair");
    }

    pid_t pid = fork();
    if (pid == -1) {
        ERR_EXIT("fork");
    }

    if (pid > 0) {
        close(sockfds[1]);
        int val = 0;
        while (1) {
            ++val;
            fprintf(stdout, "send data: %d\n", val);
            write(sockfds[0], &val, sizeof(val));
            read(sockfds[0], &val, sizeof(val));
            fprintf(stdout, "recv data: %d\n", val);
            sleep(1);
        }
    } else if (pid == 0) {
        close(sockfds[0]);
        int val = 0;
        while (1) {
            read(sockfds[1], &val, sizeof(val));
            ++val;
            write(sockfds[1], &val, sizeof(val));
        }
    }

    return 0;
}
